//
//  SLCheseStatusViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/11.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLCheseStatusViewController.h"
#import "SLChoseStatusTableViewCell.h"
#import "SLCompleteShipOwnerInformationViewController.h"



@interface SLCheseStatusViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataSource;

@property (nonatomic, strong) UIButton *certainBtn;

@end

@implementation SLCheseStatusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  [self.view addSubview:self.tableView];
  [self.view addSubview:self.certainBtn];
  [self.certainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.width.equalTo(self.view);
    make.height.equalTo(@46);
  }];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  return  self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 48;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  SLChoseStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  [cell configWithDictionary:self.dataSource[indexPath.section]];
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  SLChoseStatusTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  cell.isTaped = YES;
  [DEFAULTS setObject:self.dataSource[indexPath.section][@"type"] forKey:@"type"];
  [DEFAULTS synchronize];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
  SLChoseStatusTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  cell.isTaped = NO;
}


#pragma mark - Private Methods



- (void)nextStep{ 
  SLCompleteShipOwnerInformationViewController *completeVC;
  if ([[DEFAULTS objectForKey:@"type"]isEqualToString:@"0"]) {
    completeVC = [[SLCompleteShipOwnerInformationViewController alloc] initWithType:RegisterUserTypeShip];
  }else{
    completeVC = [[SLCompleteShipOwnerInformationViewController alloc] initWithType:RegisterUserTypeProduct];
  }
  [self.navigationController pushViewController:completeVC animated:YES];
}




#pragma mark getter

-(UITableView *)tableView{
  if (!_tableView) {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SLChoseStatusTableViewCell class] forCellReuseIdentifier:@"cell"];
  }
  return _tableView;
}


- (NSArray *)dataSource{
  if (!_dataSource) {
    _dataSource = @[@{@"statusImageView" : @"shipowner",@"statusLabel":@"船东",@"type":@"0"},@{@"statusImageView" : @"The owner",@"statusLabel":@"货主",@"type":@"1"}];
  }
  return _dataSource;
}


- (UIButton *)certainBtn{
  if (!_certainBtn) {
    _certainBtn = [UIButton buttonWithTitle:@"确定"];
    _certainBtn.backgroundColor = SLYellowColor;
    [_certainBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _certainBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [_certainBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];

  }
  return _certainBtn;
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
